package com.sunshine.lnuplus.view

import android.content.Intent
import android.graphics.Color
import android.graphics.drawable.ColorDrawable
import android.os.Bundle
import android.view.*
import androidx.fragment.app.DialogFragment
import androidx.fragment.app.Fragment
import com.google.android.material.button.MaterialButton
import com.sunshine.lnuplus.R
import com.sunshine.lnuplus.ui.activity.*
import com.sunshine.lnuplus.ui.activity.LoadFileLessonChooseActivity
import com.sunshine.lnuplus.ui.activity.LoadSchoolLessonChooseActivity
import com.sunshine.lnuplus.ui.activity.LoginActivity
import com.sunshine.lnuplus.ui.activity.SuperLoginActivity
import com.sunshine.lnuplus.utils.TagHelper


class LoginDialog : DialogFragment(){

    companion object{
        lateinit var myFragment: Fragment
        @JvmStatic
        fun newInstance(
            myFragment: Fragment,
            tag: Int) : LoginDialog{
            this.myFragment = myFragment
            val loginDialog = LoginDialog()
            val bundle = Bundle()
            bundle.putInt("tag", tag)
            loginDialog.arguments = bundle
            return loginDialog
        }
    }

    override fun onCreateView(
        inflater: LayoutInflater,
        container: ViewGroup?,
        savedInstanceState: Bundle?
    ): View? {
        val view = inflater.inflate(R.layout.dialog_login, container, false)

        view.findViewById<MaterialButton>(R.id.button_lnu_login).setOnClickListener {
            myFragment.startActivityForResult(Intent(activity, LoginActivity::class.java), arguments!!.getInt("tag"))
            dialog?.dismiss()
        }
        view.findViewById<MaterialButton>(R.id.button_class_login).setOnClickListener {
            myFragment.startActivityForResult(Intent(activity, LoadFileLessonChooseActivity::class.java), arguments!!.getInt("tag"))
            dialog?.dismiss()
        }
        view.findViewById<MaterialButton>(R.id.button_super_login).setOnClickListener {
            myFragment.startActivityForResult(Intent(activity, SuperLoginActivity::class.java), arguments!!.getInt("tag"))
            dialog?.dismiss()
        }
        view.findViewById<MaterialButton>(R.id.button_school_login).setOnClickListener {
            myFragment.startActivityForResult(Intent(activity, LoadSchoolLessonChooseActivity::class.java), arguments!!.getInt("tag"))
            dialog?.dismiss()
        }
        view.findViewById<MaterialButton>(R.id.button_json_login).setOnClickListener {
            myFragment.startActivityForResult(Intent(activity, BandRActivity::class.java), arguments!!.getInt("tag"))
            dialog?.dismiss()
        }
        val params = dialog?.window?.attributes
        params?.apply {
            gravity = Gravity.BOTTOM
            windowAnimations = R.style.bottom_animation
        }

        dialog?.window?.setBackgroundDrawable(ColorDrawable(Color.TRANSPARENT))

        return view
    }

    override fun onStart() {
        super.onStart()

        dialog?.window?.setLayout(activity!!.windowManager.defaultDisplay.width, WindowManager.LayoutParams.WRAP_CONTENT)
    }
}